package org.example.forum.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.config.AppConfig;
import org.example.forum.model.Article;
import org.example.forum.model.Board;
import org.example.forum.model.User;
import org.example.forum.service.IArticleService;
import org.example.forum.service.IBoardService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "文章接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IBoardService boardService;

    /**
     * 发布新帖子
     * @param boardId
     * @param title
     * @param content
     * @return
     */
    @ApiOperation("发布帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam @NonNull Long boardId,
                            @RequestParam @NonNull String title,
                            @RequestParam @NonNull String content) {
        //校验用户是否禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1) {
            //用户已禁言
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }
        //校验板块信息
        Board board = boardService.selectById(boardId);
        if(board==null || board.getDeleteState()==1 || board.getState()==1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            return AppResult.failure(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        //封装文章对象
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);
        article.setUserId(user.getId());
        articleService.create(article);
        return AppResult.success();
    }

    @ApiOperation("获取帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@RequestParam(required = false) Long boardId) {
        List<Article> articles;
        if(boardId==null || boardId==0) {
            articles = articleService.selectAll();
        }else {
            articles = articleService.selectAllByBoardId(boardId);
        }

        if(articles==null || articles.size()==0) {
            articles=new ArrayList<>();
        }
        return AppResult.success(articles);
    }

    @ApiOperation("获取帖子详情")
    @GetMapping("/getDetail")
    public AppResult<Article> getDetails(HttpServletRequest request,@NonNull Long id) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        Article article = articleService.selectDetailById(id);
        if(article==null) {
            return AppResult.failure(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断用户是否为作者
        if(user.getId()==article.getUserId()) {
            article.setOwn(true);
        }
        return AppResult.success(article);
    }

    @ApiOperation("修改帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,@NonNull Long id,@NonNull String title,@NonNull String content) {
        //判断用户是否处于禁言状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1) {
            return  AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if(article==null) {
            return AppResult.failure(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断用户是不是作者
        if(user.getId()!=article.getUserId()) {
            return AppResult.failure(ResultCode.FAILED_FORBIDDEN);
        }
        if(article.getState() == 1){
            return AppResult.failure(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //进行更新
        articleService.modify(id, title, content);
        log.info("帖子更新成功,ArticleId = "+id);
        return AppResult.success();
    }

    @ApiOperation("点赞帖子")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@NonNull Long id) {
        //判断用户是否处于禁言状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1) {
            return  AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if(article==null || article.getDeleteState()==1) {
            return AppResult.failure(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        articleService.thumbsUpById(id);
        return AppResult.success();
    }

    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,@NonNull Long id) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1) {
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if(article==null || article.getDeleteState()==1 ) {
            return AppResult.failure(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //校验用户是不是作者
        if(user.getId()!=article.getUserId()) {
            return AppResult.failure(ResultCode.FAILED_FORBIDDEN);
        }
        //帖子状态异常
        if(article.getState() == 1){
            return AppResult.failure(ResultCode.FAILED_ARTICLE_BANNED);
        }
        articleService.deleteById(id);
        return AppResult.success();
    }

    @ApiOperation("获取用户帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,@RequestParam(required = false) Long userId) {
        if (userId==null){
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId=user.getId();
        }
        List<Article> articles=articleService.selectByUserId(userId);
        return AppResult.success(articles);
    }

    @ApiOperation("搜索框搜索")
    @GetMapping("/search")
    public AppResult<List<Article>> search(@NonNull String keyword){
        List<Article> articles = articleService.selectBySearchBox(keyword);
        return AppResult.success(articles);
    }
}
